;(function($){
	/**
	 * 电子券扫描
	 */
	$.fn.coupon_sweep_code=function(options){
		var el = this;
		var opt = {
			gno:"",	//客户编号
    		rmtype:"",//房型
    		rate:"" ,//费率
    		arg : "rm",
    		mno : "",
    		remark_c:"",//备注
    		returnBack:function(){},//关闭回调事件
    		dataBack:function(){},//选中数据回调事件	
    		closeBack:function(){},
		}
		var param = $.extend(opt,options);
		var app = new App(el,param);
	}
	
	var App = function(el,param){
		this.el = el;
		this.param = param;
		this.initHmtl();
		this.bindEvent();
	}
	
	App.prototype = {
		bindEvent : function(){
			var el = this.el;
			var param = this.param;
			var t = this;
    		
    		//关闭
			el.find('#modalBtnCancel').bind("click",function(){
				el.modal("hide");
				el.empty();
    			param.closeBack();
    		});
			
    		//使用
			el.find("#modalBtnSweepCode").bind("click",function(e,paramer){
				t.wait();
				//构造参数
        		var pa={};
        		if(paramer){
        			pa.arg={};
        			pa.arg=$.extend({},pa.arg,paramer); 
        		}else{
        			pa.arg={arg:param.arg,operation:"actived"};
        			if(param.arg=="rm"){
        				pa.arg.gno = param.gno
        			}else if(param.arg=="pos"){
        				pa.arg.no = param.no
        			}
            		pa.arg=$.extend({},pa.arg,{"qrcode":el.find("#qrcode").val(),"mno":param.mno});   
        		}
        		
				$.ajax({
					type : 'POST',
					async:false, 
					url :$.HSAPI_AJAXURL+"/qms/mobile/procedure2",
					data : $.HSAPI_ASKJSONINFO( JSON.stringify(pa),'HoMemberCoupon_Active','{}'),
					dataType : 'json',  
					success : function(data){
						t.goOn();
    					if(data.RETURN==1){ 
							el.find("#qrcode").val("");
    						jf_success(data.MSG);
    						el.modal("hide");
    						el.empty();
    						param.dataBack();
    					}else if(data.RETURN=="-10"){ 
							el.find("#qrcode").val("");
    						t.errorContent(pa.arg,data);  
    					}else if(data.RETURN=="-11"){ 
							el.find("#qrcode").val("");
    						t.errorContent_11(pa.arg,data);  
    					}else{
							el.find("#qrcode").val("");
    						el.find("#qrcode").focus();
    						jf_alert(data.MSG);
    					}
					},  
					error : function(data){
						el.find("#qrcode").val("");
						el.find("#qrcode").focus();
						jf_alert(JSON.stringify(data))
					}  
				});
			})
			
			el.find("#qrcode").keydown(function(e){
				if(e.keyCode==13){
					if(el.find("#qrcode").val()){
						el.find("#qrcode").blur();
						el.find("#modalBtnSweepCode").trigger("click");
					}
				}
			})
			
			setTimeout(function(){
				el.find("#qrcode").focus();
			},200)
		},
		initHmtl : function(){
			var el = this.el;
			var param = this.param;
			
			this.el.addClass("modal").attr("tabindex","-2").attr("data-backdrop","static");
    		var html = 	'<div class="modal-dialog" style="top:100px;width:600px;">'+
				    		'<div id="pageOverlay" class="hide"></div>'+
				    		'<div id="loading" class="loading hide">'+						
				    			'<i class="ace-icon fa fa-spinner fa-spin orange bigger-250"></i>'+
				    		'</div>'+
							'<div class="modal-content">'+
								'<div class="modal-header no-padding">'+
									'<div class="table-header">'+
										'<span id="title-dialog">'+$.i18n.propJsp("电子券扫码")+'</span>'+
									'</div>'+
								'</div>'+
								'<div class="modal-body" style="padding:15px !important;">'+
								
									'<div class="widget-box transparent">'+
										'<div class="widget-header widget-header-small">'+
											'<h5 class="widget-title lighter">'+$.i18n.propJsp("券码")+'</h5>'+
										'</div>'+
										'<div class="widget-body">'+
											'<div class="widget-main">'+
												'<form class="form-search" onsubmit="return false;">'+
													'<div class="row">'+
														'<div class="col-xs-12">'+
															'<div class="input-group">'+
																'<input tabindex="0" id="qrcode" name="qrcode" autocomplete="off" type="text" class="form-control search-query" style="width:100%;">'+
																'<span class="input-group-btn">'+
																	'<button id="modalBtnSweepCode" type="button" class="btn btn-purple btn-sm">'+
																		'<span class="ace-icon fa fa-check icon-on-right bigger-110"></span>'+
																		$.i18n.propJsp("使用")+
																	'</button>'+
																'</span>'+
															'</div>'+
														'</div>'+
													'</div>'+
												'</form>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+	
								'<div class="modal-footer">'+
									'<div class="clearfix">'+
										'<div class="pull-right">'+
											'<div class="btn-group hide">'+
										   		'<button id="save" type="button" class="btn btn-sm btn-info">'+
													'<i class="ace-icon fa fa-check"></i>'+$.i18n.propJsp("使用")+
												'</button>'+
											'</div>'+
											'<div class="btn-group">'+
												'<button id="modalBtnCancel" type="button" class="btn btn-sm btn-default">'+
													'<i class="ace-icon fa fa-undo"></i>'+$.i18n.propJsp("关闭")+
												'</button>'+
											'</div>'+
										'</div>'+
									'</div>'+
								'</div>'+
							'</div>'+
						'</div>'+
					'</div>';
    		this.el.html(html);
		},
    	//出错提示
    	errorContent : function(pa,data){
    		var t=this;
    		var param=this.param; 
    		var el=this.el;  
    		
    		var MSG = data.MSG;
    		var name = data.checkbox;
    		
    		
    		var html = 	'<h3 class="header smaller lighter blue">'+
    						'<i class="ace-icon fa fa-bullhorn"></i>'+MSG+
    					'</h3>'+
    					'<div class="alert alert-block alert-danger">'+
    						'<label>'+
    							'<input id="checkin_cccheckbox" type="checkbox" name="'+name+'" value="" class="ace"/>'+ 
    							'<span class="lbl" style="color:#478fca;">'+MSG+'</span>'+
    						'</label>'+
    					'</div>';

    		bootbox.dialog({
    			message: html,
    			buttons:{
    				"button1":{
    					"label" : $.i18n.propJsp("确定"),
    					"className" : "btn-sm btn-info",
    					"callback" : function(){
    						if($("#checkin_cccheckbox").prop("checked")){
    							pa[name]="yes";
    						}else{
    							pa[name]="no";
    						}
    						el.find("#modalBtnSweepCode").trigger("click",pa);   
    					}
    				},	
    				"button" :
    				{
    					"label" : $.i18n.propJsp("取消"),
    					"className" : "btn-sm",
    				},
    			}
    		});
    	},
    	//出错提示-11
    	errorContent_11 : function(pa,data){
    		var t=this;
    		var param=this.param; 
    		var el=this.el;  
    		
    		var name = data.radiobutton
    		
    		$("#save").modalFrame("destroy");
			$("#save").modalFrame("init",function(modalEle){
				if(!$.isFunction($().RETURN_11)){
					$.ajaxSetup({ async: false, cache: false });
		            $.getScript("js/cashier_mana/jquery.RETURN_11.js");
		            $.ajaxSetup({ async: true });
				}
		    	$("#"+modalEle).RETURN_11({
		    		data:data,
		    		dataBack : function(data){
		    			$("#"+modalEle).modal("hide");
		    			$("#"+modalEle).empty();
		    			pa[name]=data;
		    			el.find("#modalBtnSweepCode").trigger("click",pa);   
		    		},
		    		returnBack : function(){
		    			$("#"+modalEle).modal("hide");
		    			$("#"+modalEle).empty();
		    			el.find("#modalBtnSweepCode").trigger("click",pa);   
		    		},
		    	});
			})
    					
    	},
    	//等待
    	wait:function(){
    		var el = this.el;
    		el.find("#loading").removeClass("hide");
    		el.find("#pageOverlay").removeClass("hide");
    	},
    	
    	//继续
    	goOn:function(){
    		var el = this.el;
    		el.find("#loading").addClass("hide");
    		el.find("#pageOverlay").addClass("hide");
    	}
	}
	
	
})(jQuery)